<?php
!defined ('P_W') && exit('Forbidden');

class PW_HouseVrangeDB extends BaseDB {

	var $_tableName = "pw_house_vrange";

	function add($data) {
		$this->_db->update('INSERT INTO ' . $this->_tableName . ' SET ' . pwSqlSingle($data));
	}

	function adds($data) {
		$this->_db->update("INSERT INTO pw_house_vrange (type,fieldvalue,vstart,vend,vieworder,ifopen) VALUES " . pwSqlMulti($data));
	}

	function getByType($type) {
		$query = $this->_db->query("SELECT * FROM pw_house_vrange WHERE type=" . pwEscape($type) . ' ORDER BY vieworder');
		return $this->_getAllResultFromQuery($query);
	}
	
	function getTypeList($type,$page,$perpage){
		if (!S::isNum($page) || !S::isNum($perpage) || empty($type)){
			return array();
		} 
		$offset = ($page - 1) * $perpage;
		$sql = 'SELECT * FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type) . ' ORDER BY vieworder,id '.$this->_Limit($offset,$perpage);
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
	}
	
	function getAllTypeList($type,$ifopen = 1){
		if(empty($type))return array();
		if($ifopen) $_where = ' AND ifopen = 1 ';
		$sql = 'SELECT * FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type) . $_where . ' ORDER BY vieworder,id ';
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
		
	}
	
	
	
	function getCountTypeList($type){
		
		$sql = 'SELECT count(*) FROM '.$this->_tableName.'  WHERE type=' . pwEscape($type);
		return $this->_db->get_value($sql);
		
	}

	function updateById($type, $id, $data) {
		$this->_db->update('UPDATE ' . $this->_tableName . ' SET ' . pwSqlSingle($data) . ' WHERE type=' . pwEscape($type) . ' AND id=' . pwEscape($id));
	}

	function deleteByIds($type, $ids) {
		$this->_db->update("DELETE FROM pw_house_vrange WHERE type=" . pwEscape($type) . ' AND id IN (' . pwImplode($ids) . ')');
	}
	
	function getOpenVrangeFieldList(){
		$sql = 'SELECT * FROM '.$this->_tableName.' WHERE ifopen=1 ORDER BY vieworder,id';
		$query = $this->_db->query($sql);
		return  $this->_getAllResultFromQuery($query);
	}
}
?>